package cn.baker.rabbitmq.mapper;

import cn.baker.rabbitmq.entity.TransMessage;
import org.checkerframework.checker.units.qual.C;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 消息Mapper
 * @author yuanyu
 */
public class TransMessageMapper {

    private JdbcTemplate jdbcTemplate;

    public TransMessageMapper(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 插入消息
     *
     * @param transMessage 消息
     */
    public int insert(TransMessage transMessage) {
        Object[] args = {transMessage.getId(), transMessage.getType(), transMessage.getService(), transMessage.getExchange(), transMessage.getRoutingKey(), transMessage.getQueue(), transMessage.getSequence(), transMessage.getPayload()};
        String sql = "INSERT INTO trans_message (`id` ,`type`, `service`, `exchange`, `routing_key`, `queue`, `sequence`, `payload`) " +
                     "VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, args);
    }

    /**
     * 修改消息
     *
     * @param transMessage 消息
     */
    public int update(TransMessage transMessage) {
        Object[] args = { transMessage.getType(), transMessage.getService(), transMessage.getExchange(), transMessage.getRoutingKey(), transMessage.getQueue(), transMessage.getSequence(), transMessage.getPayload() , transMessage.getId(), transMessage.getService()};
        String sql = "update trans_message set `type` = ?, `service` = ?, `exchange` = ?, `routing_key` = ?, `queue` = ?, `sequence` = ?, `payload` = ?" +
                 "where id= ? and service = ?";
        return jdbcTemplate.update(sql, args);
    }

    /**
     * 通过服务名查找消息
     *
     * @param id      消息ID
     * @param service 服务名
     * @return 消息
     */
    public TransMessage selectByIdAndService(String id, String service) {
        Object[] args = {id, service};
        String sql = "SELECT `id`, `type`, `service`, `exchange`, `routing_key` as routingKey, `queue`, `sequence`, `payload`" +
                "FROM trans_message " +
                "WHERE id = ? and service = ?";
        // 单个查询不到抛异常
        // return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TransMessage.class), args);

        List<TransMessage> transMessageList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TransMessage.class), args);
        if (transMessageList.isEmpty()) {
            return null;
        }
        Assert.isTrue(transMessageList.size() == 1, "查询出的数据不唯一");
        return transMessageList.get(0);
    }

    /**
     * 通过消息类型和服务名查找消息
     *
     * @param type    消息类型 TransMessageType
     * @param service 服务名
     * @return 消息集合
     */
    public List<TransMessage> selectByTypeAndService(String type, String service) {
        Object[] args = {type, service};
        String sql = "SELECT `id`, `type`, `service`, `exchange`, `routing_key` as routingKey, `queue`, `sequence`, `payload`" +
                "FROM `trans_message` " +
                "WHERE `type` = ? and `service` = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TransMessage.class), args);
    }

    /**
     * 通过消息ID和服务名删除消息
     *
     * @param id      消息ID
     * @param service 服务名
     */
    public int delete(String id, String service) {
        Object[] args = {id, service};
        String sql = "DELETE FROM trans_message WHERE id = ? and service = ?";
        return jdbcTemplate.update(sql, args);
    }
}
